package com.yige.service.mongoService

import com.mongodb.BasicDBObject
import com.mongodb.DBCollection
import com.mongodb.DBCursor
import com.mongodb.DBObject
import com.yige.common.SystemConstant
import com.yige.service.StudentService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

/**
 * Created by sunhao on 2015/9/20 0020.
 */

@Service
class MongoStudentService extends BasicMongoServiceImp implements StudentService{

    @Autowired
    MongoLessonStuService lessonService

    @Override
    DBCollection getCollection() {
        return mongoTemplate.getCollection(SystemConstant.MONGODB_COL_STUDENT)
    }

    @Override
    List<DBObject> findAllLessonForStudent(Object studentId) {
        DBObject student = findOneById(studentId)
        List<DBObject> lessonList = student.lessons
        return lessonList
    }

    @Override
    boolean bindLessonForStudent(Object studentId, Object lessonId) {
        List<DBObject> lessons = findAllLessonForStudent(studentId)
        def lesson = lessonService.findOneById(lessonId)
        lessons.add( [id:lessonId,name:lesson.name,price:lesson.price,n:0]as BasicDBObject)
        updateLessonForStudent(studentId,lessons)
        return true
    }

    @Override
    boolean removeLessonForStudent(Object studentId, Object lessonId) {
        List<DBObject> lessons = findAllLessonForStudent(studentId)
        def list = new ArrayList()
        lessons.each {
            if(!lessonId.equals(it.id)){
                list.add(it)
            }
        }
        updateLessonForStudent(studentId,list)
        return true
    }

    @Override
    boolean updateLessonForStudent(Object studentId, List<DBObject> lessons) {
        getCollection().update(
                [_id:studentId]as BasicDBObject,[$set:[lessons:lessons]as BasicDBObject]as BasicDBObject
        )
        return true
    }

    @Override
    List<DBObject> findAllTeacherForStudent(Object studentId) {
        DBObject student = findOneById(studentId)
        List<DBObject> teacherList = student.teachers
        return teacherList
    }

    @Override
    boolean bindTeacherForStudent(Object studentId, Object teacherId) {
        List<DBObject> teachers = findAllTeacherForStudent(studentId)
        teachers.add(teacherId)
        updateTeacherForStudent(studentId,teachers)
        return true
    }

    @Override
    boolean removeTeacherForStudent(Object studentId, Object teacherId) {
        List<DBObject> teachers = findAllTeacherForStudent(studentId)
        teachers.remove(teacherId)
        updateLessonForStudent(studentId,teachers)
        return true
    }

    @Override
    boolean updateTeacherForStudent(Object studentId, List<DBObject> teachers) {
        return getCollection().update(
                [_id:studentId]as BasicDBObject,[$set:[teachers:teachers]as BasicDBObject]as BasicDBObject
        )
    }

    @Override
    boolean updatePayInfoForStudent(Object studentId, List<DBObject> payInfo) {
        getCollection().update([_id:studentId]as BasicDBObject, [$set:[payInfo:payInfo]as BasicDBObject]as BasicDBObject)
        return true
    }

    @Override
    List<DBObject> findNoPayStudent() {
        List<DBObject> list = new ArrayList<>()
        DBObject dbObject,studentInfo
        DBCursor cursor = getCollection().find(["lessons.n": ['$lt':0]as BasicDBObject]as BasicDBObject)
        while (cursor.hasNext()){
            dbObject = cursor.next()
            dbObject.get("lessons").each {
                if(it.n < 0){
                    studentInfo = [_id:dbObject._id,name: it.name,price:it.price,n:it.n,lesson:it.id]as BasicDBObject
                    list.add(studentInfo)
                }
            }
        }
        return list
    }

    @Override
    List<DBObject> findFinishStudent() {
        List<DBObject> list = new ArrayList<>()
        DBObject dbObject,studentInfo
        DBCursor cursor = getCollection().find(["lessons.n": 0]as BasicDBObject)
        while (cursor.hasNext()){
            dbObject = cursor.next()
            dbObject.get("lessons").each {
                if(it.n == 0){
                    studentInfo = [_id:dbObject._id,name: it.name,price:it.price,n:it.n,lesson:it.id]as BasicDBObject
                    list.add(studentInfo)
                }
            }
        }
        return list
    }
}
